package com.shangxian.giveithome;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname Test
 * @Description TODO
 * @Date 2020/3/26 1:35
 * @Created by fyf
 */
@Slf4j
//声明:如果不想每次都写
// private  final Logger logger = LoggerFactory.getLogger(当前类名.class);
// 可以用注解@Slf4j;
public class Test {


    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public static List<TreeNode> generateTrees(int n) {
        if(n == 0) {
            return new ArrayList<>();
        }
        return getAns(1, n);
    }

    private static List<TreeNode> getAns(int start, int end) {
        List<TreeNode> ans = new ArrayList<>();

        //此时没有数字，将 null 加入结果中
        if(start > end) {
            ans.add(null);
            return ans;
        }

        // 当前只有一个数字，当前数字作为一个树加入结果集中
        if(start == end) {
            TreeNode tree = new TreeNode(start);
            ans.add(tree);
            return ans;
        }

        // 尝试每个数字作为根节点
        for(int i = start; i <= end; i++) {
            // 得到所有可能的左子树
            List<TreeNode> leftTrees = getAns(start, i - 1);
            // 得到所有可能的右子树
            List<TreeNode> rightTrees = getAns(i + 1, end);
            // 左子树和右子树两两结合
            for(TreeNode leftTree : leftTrees) {
                for(TreeNode rightTree : rightTrees) {
                    TreeNode root = new TreeNode(i);
                    root.left = leftTree;
                    root.right = rightTree;
                    // 加入最终结果集中
                    ans.add(root);
                }
            }

        }
        return ans;
    }

    static List<Img> imgList = new ArrayList<>();
    static class A {
        public synchronized void a() throws InterruptedException {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000);
                System.out.println("a执行" + i);
            }
        }

        public void c() {
            System.out.println("c执行");
        }
    }

    static class B {
        public void b() throws InterruptedException {
            synchronized (this) {
                d();
            }
//            for (int i = 0; i < 10; i++) {
//                Thread.sleep(100);
//                System.out.println("b执行" + i);
//            }
        }
        public synchronized void d() throws InterruptedException {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(100);
                System.out.println("d执行" + i);
            }
        }
    }

    @Data
    @AllArgsConstructor
    static class Img {
        private String path;
    }

    public static void main(String[] args) {
//        String str = "address, province, city, county";
//        printMybatisSql(str);

        moveFile("C:\\Users\\30966\\Desktop\\FileData");
        for (Img img : imgList) {
            log.info("path: {}", img.getPath());
        }
        List<TreeNode> ans = generateTrees(4);
        System.out.println(ans);
    }


    public static boolean moveFile(String path) {
        File file = new File(path);
        File[] nextFiles;
        // 图片文件夹根目录层
        if (!file.exists()) {
            log.info("图片文件夹不存在");
            return false;
        }
        nextFiles = file.listFiles();
        if (nextFiles == null || nextFiles.length == 0) {
            log.info("openId名文件夹不存在");
            return false;
        }
        // openId名层
        for (File openF : nextFiles) {
            File[] classNameFiles = openF.listFiles();
            if (classNameFiles == null || classNameFiles.length == 0) {
                continue;
            }
            String openId = openF.getName();

            // className层
            for (File cnF : classNameFiles) {
                File[] fileNameFiles = cnF.listFiles();
                if (fileNameFiles == null || fileNameFiles.length == 0) {
                    continue;
                }
                String className = cnF.getName();

                // fileName层
                for (File fnF : fileNameFiles) {
                    File[] imgFiles = fnF.listFiles();
                    if (imgFiles == null || imgFiles.length == 0) {
                        continue;
                    }
                    String fileName = fnF.getName();

                    // 具体图片层
                    for (File imgFile : imgFiles) {
                        String imgPath = openId + "/" + className + "/" + imgFile.getName() ;
                        imgList.add(new Img(imgPath));
                    }
                }
            }
        }



        return true;
    }

    public static void printMybatisSql(String str) {
        str = str.replaceAll("\\s*", "");
        String[] strArray = str.split(",");
        StringBuilder insertSb = new StringBuilder();
        StringBuilder updateSb = new StringBuilder();
        for (int i = 0, size = strArray.length; i < size; i++) {
            String s = strArray[i];
            updateSb.append(s + "=");
            int k;
            while ((k = s.indexOf("_")) != -1) {
                if (k < s.length() - 1) {
                    String index = String.valueOf(s.charAt(k + 1));
                    s = s.replaceFirst("_" + index, index.toUpperCase());
                }
            }
            if (i != size - 1) {
                insertSb.append("#{" + s + "}, ");
                updateSb.append("#{" + s + "}, ");
            } else {
                insertSb.append("#{" + s + "}");
                updateSb.append("#{" + s + "}");
            }
        }
        System.out.println("insert: " + insertSb);
        System.out.println("update: " + updateSb);
    }

}
